-
-
Notifications
You must be signed in to change notification settings - Fork 393
EffOperations #7764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev/feature
Are you sure you want to change the base?
EffOperations #7764
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i will look more closely later
src/main/java/org/skriptlang/skript/lang/arithmetic/Arithmetics.java
Outdated
Show resolved
Hide resolved
src/main/java/org/skriptlang/skript/lang/arithmetic/Arithmetics.java
Outdated
Show resolved
Hide resolved
src/main/java/org/skriptlang/skript/lang/arithmetic/Arithmetics.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
everything else looks good, though there might be an opportunity for optimization if the left expression has a return value that exactly matches an operation, then you wouldn't have to do lookups during the function. Optional, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are some initial things I noticed
src/main/java/org/skriptlang/skript/lang/arithmetic/Arithmetics.java
Outdated
Show resolved
Hide resolved
|
||
test "operations effect invalid operation": # TODO: test for runtime errors | ||
set {_date} to now | ||
multiply {_date} by 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be asserting the value of {_date} afterwards
error("Cannot operate with a null object."); | ||
return; | ||
} | ||
if (left.isSingle() && left.getSingle(event) == null) { | ||
error("Cannot operate on a null object."); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be doing errors here imo. Precedent is for effects acting on nothing to simply do nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might like to see us experiment with having this piggyback off of ExprArithmetic. That is, you create a new ExprArithmetic and initialize it with the associated matched pattern and such. You can then just evaluate the expression in execute
and change the value. If there is some major reason this wouldn't (or doesn't) work, let me know. OR, it might be better to just pull out some of the shared logic for unparsed literal/return type handling
Also, it might not hurt to have an arithmetic module :) would like to hear what others think about that though (would also provide organization for my proposal above)
divide 10 by {_num} | ||
""") | ||
@Since("INSERT VERSION") | ||
public class EffOperations extends Effect implements SyntaxRuntimeErrorProducer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not EffArithmetic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking pretty good. I'm still wondering if we need to mimic the UnparsedLiteral logic from ExprArithmetic, but I'll get back to you on that.
multiply {_timespan} by 3 | ||
""") | ||
@Example(""" | ||
# Will error due to literal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to include invalid examples
return node; | ||
} | ||
|
||
private void printArithmeticError(Class<?> left, Class<?> right) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would mark these methods as static if possible
Operator operator, | ||
Class<L> leftClass, | ||
Class<R> rightClass, | ||
Class<?>... possibleReturnTypes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might call it allowedReturnTypes
Object rightObject = right.getSingle(event); | ||
if (rightObject == null) { | ||
error("Cannot operate with a null object."); | ||
return; | ||
} | ||
if (left.isSingle() && left.getSingle(event) == null) { | ||
error("Cannot operate on a null object."); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we'd want runtime errors or not for null values (would like to hear what others think). Some operations/types have default values... do we need to be checking those here?
Description
This PR aims to add effects for multiplication, division and exponentiation operations, allowing users to perform an arithmetic operation on an variable, single or list. Rather than doing, for example
set {_int} to {_int} * 2
Does not work for literals.
Target Minecraft Versions: any
Requirements: none
Related Issues: #7763